package com.leinao.project.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.commons.Response;
import com.leinao.constant.*;
import com.leinao.job.dto.JobTagDto;
import com.leinao.job.dto.ProjectJobStatisticsDto;
import com.leinao.job.dto.ProjectJobViewDto;
import com.leinao.login.dto.LoginResultDto;
import com.leinao.model.model.Model;
import com.leinao.project.dto.*;
import com.leinao.project.model.CopyProjectResult;
import com.leinao.project.model.PaiDataSet;
import com.leinao.project.model.Project;
import com.leinao.project.model.ProjectNotes;
import com.leinao.project.service.ProjectService;
import com.leinao.util.page.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * 
 * 项目
 * 
 * @author wangshoufa
 * @date 2018年11月8日 下午2:51:40
 *
 */
@RestController
@CrossOrigin
public class ProjectController {

	private static Logger logger = LoggerFactory.getLogger(ProjectController.class);

	@Autowired
	private ProjectService projectService;

	/**
	 * 创建项目
	 * 
	 * @param request
	 * @param project
	 * @param dataSetIds
	 *            如果没修改传""
	 * @return
	 * @author wangshoufa
	 * @date 2019年3月14日
	 *
	 */
	@ApiOperation(value = "项目创建", notes = "创建项目")
	@PostMapping(value = "/project")
	@ApiImplicitParam(name = "project", value = "project", required = true, dataType = "Project")
	public Response<Project> createProject(HttpServletRequest request, @RequestBody Project project,
											@ApiParam(value = "所选模型ID", required = true) @RequestParam String modelIds,
										    @ApiParam(value = "所选数据集ID", required = true) @RequestParam String dataSetIds) {
		logger.info("project param: {},dataSetId: {}", project, dataSetIds);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null || StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		if (project == null || (StringUtils.isBlank(project.getProjectName()))) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PROJECT_NAME_NULL.getSeq(),
					ProjectErrorEnum.PROJECT_NAME_NULL.getDesc(), null);
		}
		if (!Character.isLowerCase((project.getProjectName().substring(0, 1).toCharArray())[0]) && !Character.isUpperCase((project.getProjectName().substring(0, 1).toCharArray())[0])) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PROJECT_NAME_ERROR.getSeq(),
					ProjectErrorEnum.PROJECT_NAME_ERROR.getDesc(), null);
		}
		if (project != null && project.getProjectName().length() > 25) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PROJECT_NAME_LENGTH.getSeq(),
					ProjectErrorEnum.PROJECT_NAME_LENGTH.getDesc(), null);
		}
		if (project != null && project.getProfile().length() > 100) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PROFILE_LENGTH.getSeq(),
					ProjectErrorEnum.PROFILE_LENGTH.getDesc(), null);
		}
		return projectService.createProject(project, dataSetIds, modelIds, userInfo);

	}

	/**
	 * 更新项目配置 新建时仅保存、编辑项目调用该接口
	 * 
	 * @param request
	 * @param project
	 * @param dataSetIds
	 *            如果没修改传""
	 * @return
	 * @author wangshoufa
	 * @date 2019年3月13日
	 *
	 */
	@ApiOperation(value = "操作项目", notes = "新建时仅保存、编辑项目")
	@PutMapping(value = "/project")
	@ApiImplicitParam(name = "project", value = "project", required = true, dataType = "Project")
	public Response<Project> operateProject(HttpServletRequest request, @RequestBody Project project,
											@ApiParam(value = "所选模型ID", required = false) @RequestParam String modelIds,
											@ApiParam(value = "所选数据集ID", required = false) @RequestParam String dataSetIds) {
		logger.info("project param: {}, dataSetId: {}", project, dataSetIds);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null || StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		if (StringUtils.isBlank(String.valueOf(project.getId()))
				|| !StringUtils.isNumeric(String.valueOf(project.getId()))) {
			return Response.ok(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PARAM_ERROR.getSeq(), ProjectErrorEnum.PARAM_ERROR.getDesc(),
					null);
		}
		return projectService.operateProject(project, dataSetIds, modelIds, userInfo);
	}

	@ApiOperation(value = "仅保存任务", notes = "新建时仅保存")
	@PutMapping(value = "/saveJob")
	@ApiImplicitParam(name = "saveJob", value = "project", required = true, dataType = "Project")
	public Response<Project> saveJob(HttpServletRequest request, @RequestBody Project project) {

		logger.info("project param: {}", project);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null || StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		if (StringUtils.isBlank(String.valueOf(project.getId()))
				|| !StringUtils.isNumeric(String.valueOf(project.getId()))) {
			return Response.ok(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PARAM_ERROR.getSeq(), ProjectErrorEnum.PARAM_ERROR.getDesc(),
					null);
		}
		return projectService.saveJob(project, userInfo);
	}

	@ApiOperation(value = "项目名是否可用", notes = "项目名是否可用")
	@GetMapping(value = "/project/isUsable4Name")
	public Response<Void> isUsable4Name(@ApiParam(value = "项目ID", required = true) @RequestParam String projectName,
			HttpServletRequest request) {
		logger.info("isUsable4Name param projectName: {}", projectName);

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.isUsable4Name(projectName, userInfo);
	}

	@ApiOperation(value = "获取项目列表", notes = "返回项目列表")
	@GetMapping(value = "/project")
	public Response<PageInfo> project(HttpServletRequest req,
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer cp,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer ps,
			@ApiParam(value = "项目创建来源：null-所有，0-自己创建，1-复制") @RequestParam(required = false) Integer origin) {
		cp = null == cp ? Constant.DEFAULT_PAGE_NUMBER : cp;
		ps = null == ps ? Constant.DEFAULT_PAGE_SIZE : ps;
		String obj = (String) req.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null || StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		return projectService.getProjectList(userInfo.getUserId(), origin, cp, ps);
	}

	@ApiOperation(value = "获取项目明细", notes = "返回项目明细")
	@GetMapping(value = "/projectDetail")
	public Response<PageInfo> projectJobInfo(HttpServletRequest request,
			@ApiParam(value = "项目Id", required = false) @RequestParam(required = false) Long projectId,
			@ApiParam(value = "项目tag", required = false) @RequestParam(required = false) String jobTag,
			@ApiParam(value = "项目状态", required = false) @RequestParam(required = false) String taskStatus,
			@ApiParam(value = "展示任务当前页", required = false) @RequestParam(required = false) Integer cp,
			@ApiParam(value = "展示任务每页条数", required = false) @RequestParam(required = false) Integer ps) {
		cp = null == cp ? Constant.DEFAULT_PAGE_NUMBER: cp;
		ps = null == ps ? Constant.DEFAULT_PAGE_SIZE: ps;
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null || StringUtils.isBlank(userInfo.getToken())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		return projectService.getProjectJobInfo(projectId,jobTag, taskStatus,userInfo.getToken(), cp, ps);
	}

	@ApiOperation(value = "获取任务视图列表", notes = "返回任务视图列表")
	@GetMapping(value = "/project/getTaskViewList")
	public Response<PageInfo> getTaskViewList(
			@ApiParam(value = "任务状态：根据任务状态过滤数据（null或空字符串: 所有数据，WAITING: 正在等待的任务，RUNNING: 正在运行的任务，STOPPED: 暂定的任务，SUCCEEDED: 已结束的任务，FAILED：失败）") @RequestParam(required = false) String taskStatus,
			@ApiParam(value = "任务标签") @RequestParam(required = false) String jobTag,
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer pageSize, HttpServletRequest request) {
		logger.info("getTaskViewList param  pageNumber: {}, pageSize: {}", pageNumber, pageSize);

		// 默认分页条件设置
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = Constant.DEFAULT_PAGE_NUMBER;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = Constant.DEFAULT_PAGE_SIZE;
		}

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.getTaskViewList(taskStatus,jobTag,pageNumber, pageSize, userInfo);
	}

	@ApiOperation(value = "使用该数据集的项目列表", notes = "使用该数据集的项目列表")
	@GetMapping(value = "/project/getProjectListByDatasetId")
	public Response<PageInfo> getProjectListByDatasetId(
			@ApiParam(value = "数据集ID", required = true) @RequestParam Long datasetId,
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer pageSize, HttpServletRequest request) {
		logger.info("getProjectListByDatasetId param  pageNumber: {}, pageSize: {}, datasetId: {}", pageNumber,
				pageSize, datasetId);

		// 默认分页条件设置
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = Constant.DEFAULT_PAGE_NUMBER;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = Constant.DEFAULT_PAGE_SIZE;
		}

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		// 获取数据集信息
		PaiDataSet dataSet = projectService.getDatasetByDatasetId(datasetId);
		if (dataSet == null) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.INVALID_DATASET_ID.getSeq(),
					ProjectErrorEnum.INVALID_DATASET_ID.getDesc(), null);
		}
		if (DatasetTypeEnum.PUBLIC.getType().equals(dataSet.getType())) {
			return projectService.getProjectListByPublicDatasetId(datasetId, userInfo.getUserId(), pageNumber,
					pageSize);
		} else {
			return projectService.getProjectListByPrivateDatasetId(datasetId, userInfo.getUserId(), pageNumber,
					pageSize);
		}
	}

	@ApiOperation(value = "公开项目", notes = "公开项目")
	@PostMapping(value = "/project/openProject")
	public Response<Project> openProject(@RequestBody PublicProjectParamDto publicProjectParamDto, HttpServletRequest request) {
		logger.info("openProject param publicProjectParamDto: {}", publicProjectParamDto);

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		// 项目ID必填
		if(publicProjectParamDto.getProjectId() == null || publicProjectParamDto.getProjectId() == 0) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.INVALID_PROJECT_ID.getSeq(), ProjectErrorEnum.INVALID_PROJECT_ID.getDesc(),null);
		}

		// 封面ID必填
		if(publicProjectParamDto.getFileId() == null || publicProjectParamDto.getFileId() == 0) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.INVALID_FILE_ID.getSeq(), ProjectErrorEnum.INVALID_FILE_ID.getDesc(),null);
		}

		return projectService.openProject(publicProjectParamDto, userInfo);
	}

	@ApiOperation(value = "复制项目", notes = "复制项目")
	@PostMapping(value = "/project/copyProject")
	public Response<Long> copyProject(
			@ApiParam(value = "所选任务ID", required = true) @RequestParam String jobIds,
			@ApiParam(value = "项目ID", required = true) @RequestParam Long projectId, HttpServletRequest request) {
		logger.info("copyProject param projectId: {}, jobIds: {}", projectId);

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		if(StringUtils.isBlank(jobIds)){
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectEnum.PROJECT_JOB_IDS_IS_EMPTY.getSeq(),
					ProjectEnum.PROJECT_JOB_IDS_IS_EMPTY.getDesc(), null);
		}

		if(null == projectId){
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectEnum.PROJECT_ID_IS_EMPTY.getSeq(),
					ProjectEnum.PROJECT_ID_IS_EMPTY.getDesc(), null);
		}

		//复制项目准备数据
		Response<CopyProjectResult> copyProjectResult = projectService.copyProject(projectId, jobIds, userInfo);
        logger.info("copyProject param  copyProjectResult: {}", copyProjectResult);
        if(copyProjectResult.getMessage().getStatus() != Constant.RESPONSE_STATUS_SUCCESS){
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectEnum.COPY_DATA_IS_ERROR.getSeq(),
					ProjectEnum.COPY_DATA_IS_ERROR.getDesc(), null);
		}
		//初始化仓库失败
		if(copyProjectResult.getMessage().getCode() == ProjectErrorEnum.REPOSITORY_INIT_FAILURE.getSeq()){
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.REPOSITORY_INIT_FAILURE.getSeq(),
					ProjectErrorEnum.REPOSITORY_INIT_FAILURE.getDesc(), null);
		}
		return projectService.copyProjectInsert(copyProjectResult.getData(), userInfo);
	}

	@ApiOperation(value = "公开项目列表", notes = "公开项目列表")
	@GetMapping(value = "/project/getPublicProjectList")
	public Response<PageInfo> getPublicProjectList(
			@ApiParam(value = "排序方式", required = true) @RequestParam Integer sortType,
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer pageSize,
			@ApiParam(value = "分类ID") @RequestParam(required = false) Long categoryId, HttpServletRequest request) {
		logger.info("getPublicProjectList param  pageNumber: {}, pageSize: {}, sortType: {}, categoryId: {}",
				pageNumber, pageSize, sortType, categoryId);

		// 默认分页条件设置
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = Constant.DEFAULT_PAGE_NUMBER;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = Constant.DEFAULT_PAGE_SIZE;
		}

		// 校验排序类型
		if (!PublicProjectSortTypeEnum.containsType(sortType)) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.INVALID_SORT_TYPE.getSeq(),
					ProjectErrorEnum.INVALID_SORT_TYPE.getDesc(), null);
		}

		return projectService.getPublicProjectList(sortType, categoryId, pageNumber, pageSize);
	}

	@ApiOperation(value = "公开项目统计", notes = "公开项目统计")
	@GetMapping(value = "/project/getPublicProjectCollectInfo")
	public Response<PublicProjectCollectDto> getPublicProjectCollectInfo() {
		logger.info("getPublicProjectCollectInfo param");

		return projectService.getPublicProjectCollectInfo();
	}

	@ApiOperation(value = "我的项目统计", notes = "我的项目统计")
	@GetMapping(value = "/project/getProjectCollectInfo")
	public Response<ProjectCollectDto> getProjectCollectInfo(HttpServletRequest request) {
		logger.info("getProjectCollectInfo param");

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.getProjectCollectDto(userInfo.getUserId());
	}

	@ApiOperation(value = "编辑项目", notes = "编辑项目")
	@PostMapping(value = "/project/editProject")
	@ApiImplicitParam(name = "project", value = "project", required = true, dataType = "Project")
	public Response<Void> editProject(@RequestBody Project project, HttpServletRequest request) {
		logger.info("editProject param project: {}", project);

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.editProject(project, userInfo.getUserId());
	}

	@ApiOperation(value = "查询项目笔记", notes = "查询项目笔记")
	@GetMapping(value = "/project/getProjectNotes")
	public Response<ProjectNotes> getProjectNotes(
			@ApiParam(value = "项目ID", required = true) @RequestParam Long projectId, HttpServletRequest request) {
		logger.info("getProjectNotes param projectId: {}", projectId);

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.getProjectNotes(projectId, userInfo.getUserId());
	}

	@ApiOperation(value = "编辑项目笔记", notes = "编辑项目笔记")
	@PostMapping(value = "/project/editProjectNotes")
	@ApiImplicitParam(name = "projectNotes", value = "projectNotes", required = true, dataType = "projectNotes")
	public Response<Void> editProjectNotes(@RequestBody ProjectNotes projectNotes, HttpServletRequest request) {
		logger.info("editProjectNotes param projectNotes: {}", projectNotes);

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.editProjectNotes(projectNotes, userInfo.getUserId());
	}

	@ApiOperation(value = "获取公开项目明细", notes = "返回公开项目明细")
	@GetMapping(value = "/project/getPublicProjectDetail")
	public Response<Project> getPublicProjectDetail(
			@ApiParam(value = "项目ID", required = true) @RequestParam Long projectId) {
		logger.info("getPublicProjectDetail param projectId: {}", projectId);

		return projectService.getPublicProjectDetail(projectId);
	}

	@ApiOperation(value = "搜索公开项目列表", notes = "搜索公开项目列表")
	@GetMapping(value = "/project/searchPublicProjectList")
	public Response<PageInfo> searchPublicProjectList(
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer pageSize,
			@ApiParam(value = "搜索内容") @RequestParam String search, HttpServletRequest request) {
		logger.info("searchPublicProjectList param  pageNumber: {}, pageSize: {}, search: {}", pageNumber, pageSize,
				search);

		// 默认分页条件设置
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = Constant.DEFAULT_PAGE_NUMBER;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = Constant.DEFAULT_PAGE_SIZE;
		}

		return projectService.searchPublicProjectList(search, pageNumber, pageSize);
	}

	@ApiOperation(value = "获取任务不同状态统计结果", notes = "返回任务不同状态统计结果")
	@GetMapping(value = "/project/getTaskStatistics")
	public Response<ProjectJobStatisticsDto> getTaskViewList(HttpServletRequest request) {

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.getTaskStatistics(userInfo);
	}

	@ApiOperation(value = "获取公开项目事例", notes = "获取公开项目事例")
	@GetMapping(value = "/project/getPublicProjectExample")
	public Response<PublicProjectExampleDto> getPublicProjectExample() {
		return projectService.getPublicProjectExample();
	}

    @ApiOperation(value = "获取用户项目详情", notes = "获取用户项目详情")
    @GetMapping(value = "/project/getUserProjectDetail")
    public Response<Project> getUserProjectDetail(HttpServletRequest request,
                 @ApiParam(value = "项目Id", required = false) @RequestParam(required = false) Long projectId) {
        logger.info("getPublicProjectDetail param projectId: {}", projectId);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return projectService.getUserProjectDetail(projectId, userInfo);
    }

	@ApiOperation(value = "获取用户任务详情", notes = "获取用户任务详情")
	@GetMapping(value = "/project/getUserTaskDetail")
	public Response<ProjectJobViewDto> getUserTaskDetail(HttpServletRequest request,
								@ApiParam(value = "任务名", required = true) @RequestParam Long jobId){

		logger.info(String.format("jobOperation jobId：", jobId));
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null && StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}

		return projectService.getUserTaskDetail(jobId, userInfo);
	}

	@ApiOperation(value = "添加标签", notes = "添加标签")
	@PostMapping(value = "/project/saveJobTag")
	public Response<ProjectJobViewDto> saveJobTag(HttpServletRequest request,
								@ApiParam(value = "项目ID", required = true) @RequestParam Long projectId,
								@ApiParam(value = "任务ID", required = true) @RequestParam Long jobId,
								@ApiParam(value = "标签", required = true) @RequestParam String jobTag){

		logger.info("projectId: {}, jobId: {}, jobTag: {}：", projectId,jobId,jobTag);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null && StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		if(jobTag.length()>Constant.JOBTAG_LENGTH){
		    return Response.error(Constant.RESPONSE_STATUS_SUCCESS,ProjectJobHandleEnum.JOBTAG_TOO_LONG.getSeq(),ProjectJobHandleEnum.JOBTAG_TOO_LONG.getDesc(),null);
        }
		projectService.saveJobTag(projectId,jobId,userInfo.getUserId(),jobTag);
		return Response.ok(null);
	}

	@ApiOperation(value = "修改标签", notes = "修改标签")
	@PostMapping(value = "/project/updateJobTag")
	public Response<ProjectJobViewDto> updateJobTag(HttpServletRequest request,
								@ApiParam(value = "项目ID", required = true) @RequestParam Long projectId,
								@ApiParam(value = "任务ID", required = true) @RequestParam Long jobId,
								@ApiParam(value = "标签", required = true) @RequestParam String jobTag){
		logger.info("projectId: {}, jobId: {}, jobTag: {}：", projectId,jobId,jobTag);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null && StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
		//标签长度限制16
		if(jobTag.length()>Constant.JOBTAG_LENGTH){
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS,ProjectJobHandleEnum.JOBTAG_TOO_LONG.getSeq(),ProjectJobHandleEnum.JOBTAG_TOO_LONG.getDesc(),null);
		}
		projectService.updateJobTag(projectId,jobId,userInfo.getUserId(),jobTag);
		return Response.ok(null);
	}

	@ApiOperation(value = "获取标签列表", notes = "获取标签列表")
	@GetMapping(value = "/project/getjobTagListByProId")
	public Response<List<JobTagDto>> getjobTagListByProId(HttpServletRequest request,
                                                          @ApiParam(value = "项目ID", required = true) @RequestParam Long projectId){
		logger.info("projectId: {}：", projectId);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null && StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
        Response<List<JobTagDto>> listResponse = projectService.getjobTagListByProId(projectId);
        return listResponse;
	}

	@ApiOperation(value = "获取标签列表", notes = "获取标签列表")
	@GetMapping(value = "/project/getjobTagListByUserId")
	public Response<List<JobTagDto>> getjobTagListByProId(HttpServletRequest request,
                                                          @ApiParam(value = "用户ID", required = true) @RequestParam String userId){
		logger.info("userId: {}：", userId);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null && StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
        Response<List<JobTagDto>> listResponse = projectService.getjobTagListByUserId(userId);
        return listResponse;
	}

	@ApiOperation(value = "校验待公开项目是否包含私有数据集和模型", notes = "校验待公开项目是否包含私有数据集和模型")
	@GetMapping(value = "/project/getPrivateInfo")
	public Response<ProjectPrivateInfo> getPrivateInfo(HttpServletRequest request,
                                                          @ApiParam(value = "用户ID", required = true) @RequestParam String userId,
                                                          @ApiParam(value = "项目ID", required = true) @RequestParam Long id){
		logger.info("userId: {}：projectId：{}", userId,id);
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		if (userInfo == null && StringUtils.isBlank(userInfo.getUserId())) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
		}
        return projectService.getPrivateInfo(userId,id);
	}

	/**
	 * 目前未使用，产品停止该功能，接口保留
	 */
	@ApiOperation(value = "获取某个项目任务列表信息" ,notes = "创建任务页面使用")
	@PostMapping(value = "/getProjectOutputs")
	public Response<ProjectInfoDto> getProjectOutputs(HttpServletRequest request,
																@ApiParam(value = "选中项目Id", required = true) @RequestParam Long projectId){

		logger.info(String.format("getProjectsForOutputs projectId：", projectId));
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return projectService.getProjectOutputs(projectId,userInfo.getUserId());
	}

	/**
	 * 目前未使用，产品停止该功能，接口保留
	 */
	@ApiOperation(value = "获取项目的引用output路径，主要用于已经使用引用过的outputs" ,notes = "创建任务页面使用")
	@PostMapping(value = "/projectQuoteInfo")
	public Response<List<ProjectInfoDto>>  projectQuoteInfo(HttpServletRequest request,
													  @ApiParam(value = "当前任务的项目Id") Long projectId){
		logger.info(String.format("getProjectOutputs projectId：", projectId));
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return projectService.projectQuoteInfo(userInfo.getUserId(),projectId);
	}


	/**
	  * 功能描述：获取公开项目相关信息，展示项目公开历史、成功任务等信息
	  * @param request req请求
	  * @param projectId 待公开项目id
	  * @Return:com.leinao.commons.Response<com.leinao.project.dto.PublicProjectInfo> 返回前端信息
	  * @Author: Guo Liangbo
	  * @Date:2019/6/4 14:10
	  */
	@ApiOperation(value = "获取公开项目相关信息，展示项目公开历史、成功任务等信息" ,notes = "公开项目页面使用")
	@PostMapping(value = "/publicProjectInfo")
	public Response<PublicProjectInfo>  publicProjectInfo(HttpServletRequest request,
															@ApiParam(value = "当前任务的项目Id")@RequestParam Long projectId){
		logger.info(String.format("getProjectOutputs projectId：", projectId));
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return projectService.publicProjectInfo(userInfo,projectId);
	}

	@ApiOperation(value = "获取最近新建的jupyter任务配置", notes = "获取最近新建的jupyter任务配置")
	@GetMapping(value = "/project/getLastJupyterJobConfig")
	public Response<String> getLastJupyterJobConfig(HttpServletRequest request){
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return projectService.getLastJupyterJobConfig(userInfo.getUserId());
	}

    @ApiOperation(value = "查询项目引用的模型" ,notes = "编辑项目页面使用")
    @PostMapping(value = "/getProjectUseModels")
    public Response<List<Model>>  getProjectUseModels(HttpServletRequest request,
                                                      @ApiParam(value = "当前任务的项目Id") Long projectId){

        logger.info(String.format("getProjectUseModels projectId：", projectId));
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        if (null == userInfo && StringUtils.isBlank(userInfo.getUserId())) {
            return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
        }
        return projectService.getProjectUseModels(projectId);
    }

	@ApiOperation(value = "使用个人该数据集的项目列表", notes = "使用个人该数据集的项目列表")
	@GetMapping(value = "/project/getUserProjectListByDatasetId")
	public Response<PageInfo> getUserProjectListByDatasetId(
			@ApiParam(value = "数据集ID", required = true) @RequestParam Long datasetId,
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer pageSize, HttpServletRequest request) {
		logger.info("getUserProjectListByDatasetId param  pageNumber: {}, pageSize: {}, datasetId: {}", pageNumber,
				pageSize, datasetId);

		// 默认分页条件设置
		if (pageSize == null || pageSize < 1) {
			pageSize = Constant.DEFAULT_PAGE_SIZE;
		}
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = Constant.DEFAULT_PAGE_NUMBER;
		}

		// 获取登录用户信息
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		// 获取数据集信息
		PaiDataSet dataSet = projectService.getDatasetByDatasetId(datasetId);
		if (dataSet == null) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.INVALID_DATASET_ID.getSeq(),
					ProjectErrorEnum.INVALID_DATASET_ID.getDesc(), null);
		}

		return projectService.getProjectListByPrivateDatasetId(datasetId, userInfo.getUserId(), pageNumber,pageSize);
	}
}
